<template>
	<view class="p-2">
		<u-navbar title="确认订单" placeholder autoBack></u-navbar>
		<!-- 有默认选择收货人地址 -->
		<template v-if="path">
			<commonPath :item="path" @click="choosePath" style="border-radius: 16rpx;"></commonPath>
		</template>
		<!-- 无收货人地址 -->
		<template v-else>
			<view class="bg-white p-2 border-bottom flex align-center" style="border-radius: 16rpx;" @click="choosePath">
				<u-image src="/static/address.png" width="22" height="22"></u-image>
				<view class="text-light9 font-28 ml-2">选择收货地址</view>
				<view class="ml-auto">
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
		</template>
		<!-- 商品信息 -->
		<view class="bg-white p-2 mt-2" style="border-radius: 16rpx 16rpx 0 0;">
			<orderItem :item="goodsInfo"></orderItem>
		</view>
		<u-line color="#f9f9f9"></u-line>
		<view class="bg-white p-2">
			<view class="flex align-center justify-between" style="height: 60rpx;">
				<view class="font-28 text-light6">商品总价</view>
				<view class="font-30 text-main">¥{{goodsInfo.price}}</view>
			</view>
			<view class="flex align-center justify-between" style="height: 60rpx;"
			@click="clickCoupon">
				<view class="font-28 text-light6">优惠卷</view>
				<view class="font-30 text-main"><span v-if="couponItem.money">￥</span>{{couponItem.money?couponItem.money:'暂未使用优惠卷'}}</view>
			</view>
			<!-- <view class="flex align-center justify-between" style="height: 60rpx;">
				<view class="font-28 text-light6">开具发票</view>
				<commonPrice :price="price"></commonPrice>
				<view class="ml-auto">
					<u-text v-if="!invoice.invoice_title" suffixIcon="arrow-right" text="选择发票类型" size="12"
						:iconStyle="{color:'#757575'}" @click="show=true"></u-text>
					<u-text v-else suffixIcon="arrow-right" :text="invoice.invoice_title===1?'个人':'单位'" size="12"
						:iconStyle="{color:'#757575'}" @click="show=true"></u-text>
				</view>
			</view> -->
		</view>
		<u-line color="#f9f9f9"></u-line>
		<view class="bg-white p-2 flex align-center" style="border-radius: 0 0 16rpx 16rpx;">
			<view class="text-main font-28 font-weight-bold">实付款</view>
			<view class="ml-auto font-24" style="color: #e65350;font-weight: bold;">
				￥
				<text style="font-size: 28rpx;">{{price}}</text>
			</view>
		</view>
		<!-- 备注 -->
		<view class="bg-white mt-2" style="border-radius: 16rpx;display: flex;flex-direction: column;">
			<view class="font-28 text-light6 px-2 mt-2">备注</view>
			<!-- <u-input inputAlign="right" placeholder="请输入备注" border="none" :customStyle="{height:'60rpx'}"></u-input> -->
            <u-textarea placeholder="请输入填写备注信息" v-model="remarks" border="none"></u-textarea>   		
		</view>
		
		<view class="" style="height: 100rpx;"></view>
		<!-- 底部 -->
		<view class="fixed-bottom flex align-center justify-between bg-white px-2" style="height: 100rpx;">
			<view class="flex align-center justify-center font-weight-bold">
				<view class="text-main font-28 ">共1件 应付：</view>
				<commonPrice :price="price"></commonPrice>
			</view>
			<view class="sunmint" @click="submint">
				立即支付
			</view>
		</view>
		
		<!-- 弹出层 -->
	    <electronicInvoice :show="show" @click="invoice" ></electronicInvoice>
	</view>
	
</template>

<script>
	export default{
		data(){
			return{
				goodsInfo:{},
				freightprice:0,
				path:'',
				ziti:false,
				freight:0,
				invoice: [],
				show:false, // 支付发票弹框
				remarks: '' ,// 备注
				couponItem:{} // 优惠卷信息
			}
		},
		computed: {
			paytype() {
				if (uni.$u.platform === 'mp') {
					return this.curIndex === 0 ? '微信' : `余额(${this.money})`
		
				} else {
					return this.curIndex === 0 ? '支付宝' : this.curIndex === 1 ? '微信' : `余额(￥${this.money})`
				}
			},
			// 最终价格
			price() {
				if(this.couponItem.money){
					let total = this.goodsInfo.price*100*this.goodsInfo.goods_num
					total = Number((total/100).toFixed(2)) - Number(this.couponItem.money)
					total = total<=0?0:total
					return total
				}else{
					let total = this.goodsInfo.price*100*this.goodsInfo.goods_num
					return Number((total/100).toFixed(2))
				}
			},
		},
		mounted() {
			uni.$on('chooseCoupon', res => {
				console.log(res)
				this.couponItem = res
				// this.getfreightPrice(this.path.address_id)
			})
			uni.$on('choosePath', res => {
				this.path = res
				// this.getfreightPrice(this.path.address_id)
			})
			// 关闭弹窗
			uni.$on('invoice', (data) => {
				this.show = data.show
			})
			// 发票弹窗确认
			uni.$on('invoiceComfirm', (invoice) => {
				console.log(invoice)
				this.invoice = invoice.invoice
				this.show = invoice.show
			})
		},
		onLoad(e) {
			console.log(e)
			this.goodsInfo = e
			this.getdefaultPath()
		},
		methods:{
			clickCoupon(){
				this.$u.route('pages/user/coupon?isChoose=创建订单页面',{
					type:'choose'
				})
			},
			choosePath() {
				this.$u.route('pages/user/path',{
					type:'choose'
				})
			},
			getdefaultPath(){
				this.$api.addresslists().then(res=>{
					if(res.code === 1){
						this.path = res.data.length > 0 ? res.data[0] : false
						if(res.data.length>0){
							// this.getfreightPrice(this.path.address_id)
						}
					}
				})
			},
			getfreightPrice(addressId){
				let goods = [{
					goods_id: this.goodsInfo.goods_id,
					item_id: this.goodsInfo.item_id,
					num: this.goodsInfo.goods_num,
				}]
				
				let parmar = {
					type:'buy_now',
					goods:JSON.stringify(goods),
					address_id:addressId,
					cart_id:0,
				}
				
				this.$api.freightprice(parmar).then(res=>{
					if(res.code === 1){
						this.freight = res.data
						// this.freightprice = res.data
					}
				})
				
			},
			// change(e){
			// 	this.ziti = e
			// 	if(this.ziti){
			// 		this.freightprice = 0
			// 	}else{
			// 		this.freightprice = this.freight
			// 	}
				
			// },
			submint(){
				if(!this.path){
					return uni.$u.toast('请选择收货地址')
				}
				let goods = [{
					goods_id: this.goodsInfo.goods_id,
					item_id: this.goodsInfo.item_id,
					num: this.goodsInfo.goods_num,
					
				}]
				let parmar = {
					type:'buy_now',
					goods:JSON.stringify(goods),
					pay_way:1,
					address_id:this.path.address_id,
					remarks:this.remarks,
					cart_id:0,
					totalPrice:this.price,
					logistics:2,
					invoice:JSON.stringify(this.invoice),
					remarks: this.remarks,
					coupon_id:this.couponItem.id
				}
				// 生成订单
				this.$api.submitorder(parmar).then(res=>{
					console.log(res)
					if(res.code === 1){
						this.orderPay(res.data.order_id)
					}else if(res.code == '-301' ){
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/user/index'
							})
						}, 1000)
					}
				})
			},
			orderPay(orderId) {
				let parmar = {
					order_id:orderId,
					pay_way: 1
				}
				if(uni.$u.platform === 'mp'){
				    /* this.$api.wxpay(parmar).then(res=>{
				    	if(res.code === 1){
				    		uni.requestPayment({
				    			provider:'wxpay',
				    			orderInfo:res.data,
				    			complete: (result) => {
				    				setTimeout(()=>{
				    					uni.$u.route({
				    						type:'redirectTo',
				    						url:'pages/user/order'
				    					})
				    				},500)
				    			}
				    		})
				    	}
				    }) */
					this.$api.wxpay(parmar).then(res=>{
						if(res.code === 1){
							uni.requestPayment({
								provider:'wxpay',
								timeStamp:res.data.timeStamp,
								nonceStr:res.data.nonceStr,
								package:res.data.package,
								signType:res.data.signType,
								paySign:res.data.paySign,
								success: (res) => {
									uni.$u.route({
										type:'redirectTo',
										url:'pages/user/order'
									})
								}
							})
						}
						
					})
				}
			}
		}

	}
</script>

<style>
	.sunmint {
		display: flex !important;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		background: linear-gradient(123deg, #ff5d26 0%, #ff5d26 100%);
		color: white;
		border-radius: 12rpx;
		width: 230rpx;
		height: 78rpx;
		margin-left: 10rpx;
		font-size: 36rpx;
	}
		
	
</style>